﻿using RpcClient.Interface;

namespace RpcModular.Config
{
    internal class ModularConfig
    {

        static ModularConfig()
        {
            RpcClient.RpcClient.Config.AddRefreshEvent(_Refresh);
            RpcClient.RpcClient.InitComplate += RpcClient_InitComplate;
        }

        private static void RpcClient_InitComplate()
        {
            if (ModularConfig.AccreditRoleType == null)
            {
                ModularConfig.AccreditRoleType = RpcClient.RpcClient.Config.GetConfigVal("rpcassembly:Accredit:RoleType", RpcClient.RpcClient.GroupTypeVal);
            }
        }

        /// <summary>
        /// 授权配置
        /// </summary>
        public static AccreditConfig Accredit
        {
            get;
            private set;
        }
        /// <summary>
        /// 资源上传范围
        /// </summary>
        public static RpcModularModel.Resource.ResourceType UpRange
        {
            get;
        } = RpcClient.RpcClient.Config.GetConfigVal("rpcassembly:Resource:UpRange", (RpcModularModel.Resource.ResourceType)6);
        /// <summary>
        /// 授权角色类型
        /// </summary>
        public static string AccreditRoleType { get; private set; }

        private static void _Refresh(IConfigServer config, string name)
        {
            if (name == string.Empty || name.StartsWith("rpcassembly:Accredit"))
            {
                ModularConfig.Accredit = config.GetConfigVal<AccreditConfig>("rpcassembly:Accredit", new AccreditConfig());
                if (RpcClient.RpcClient.CurrentSource != null)
                {
                    ModularConfig.AccreditRoleType = config.GetConfigVal("rpcassembly:Accredit:RoleType", RpcClient.RpcClient.GroupTypeVal);
                }
            }
        }
        public static VisitConfig GetVisitConfig()
        {
            return RpcClient.RpcClient.Config.GetConfigVal("rpcassembly:Visit", new VisitConfig());
        }
        public static IdentityConfig GetIdentityConfig()
        {
            return RpcClient.RpcClient.Config.GetConfigVal("rpcassembly:Identity", new IdentityConfig());
        }
    }
}
